// pages/app/questions/questions.js
const app = getApp();
var host = app.globalData.host;
var utils = app.globalData.utils;
var self = null;

Page({
  data: {
    themeColor: '',
    defaultColor: '',
    borderColor: '',
    schoolId: '',
    list: [],
    keyword: ''
  },
  onLoad: function (options) {
    self = this;
    self.getStyle();
    self.setData({
      schoolId: options.schoolId
    })
    self.init();
  },
  getStyle() {
    self.setData({
      themeColor: app.globalData.themeColor,
      defaultColor: app.globalData.defaultColor,
      borderColor: app.globalData.borderColor
    })
  },
  init() {
    var url = host + '/lx/mini/school/question';
    var params = {
      schoolId: self.data.schoolId
    };
    if (self.data.keyword) {
      params.keyword = self.data.keyword;
    }
    var success = function (res) {
      self.setData({
        list: res.info
      })
    }
    var fail = function () { };
    var complete = function () { };
    utils.request(url, params, success, fail, complete);
  },
  toggleHidden(event) {
    var index = Number(event.currentTarget.dataset.index);
    var questionId = event.currentTarget.dataset.id;
    var isShow = self.data.list[index].show ? self.data.list[index].show : false;
    var list = self.data.list;
    list[index].show = !isShow;
    self.setData({
      list: list
    })

    if (!isShow) {
      wx.request({
        url: host + '/lx/mini/school/clickQuestion',
        data: {
          questionId: questionId
        }
      })
    }
  },
  onKeywordChange(e) {
    self.setData({
      keyword: e.detail.value
    })
  },
  clearKeyword() {
    self.setData({
      keyword: ''
    })
    self.init();
  },
  search(e) {
    self.init();
  }
})